HackerRank 3D Surface Area
提出
code: python
import math
import os
import random
import re
import sys
#
# Complete the 'surfaceArea' function below.
#
# The function is expected to return an INTEGER.
# The function accepts 2D_INTEGER_ARRAY A as parameter.
#
def surfaceArea(A, H, W):
# Write your code here
# print(A)
# 9, (1+2+4), 9, (1+3+4), (1+2+1), (4+3+4), 5, 7
first = H * W
second = 0
for i in A:
if __name__ == '__main__':
first_multiple_input = input().rstrip().split()
H = int(first_multiple_input0) W = int(first_multiple_input1) A = []
for _ in range(H):
A.append(list(map(int, input().rstrip().split())))
result = surfaceArea(A, H, W)
fptr.write(str(result) + '\n')
fptr.close()
code: python
import math
import os
import random
import re
import sys
#
# Complete the 'surfaceArea' function below.
#
# The function is expected to return an INTEGER.
# The function accepts 2D_INTEGER_ARRAY A as parameter.
#
def surfaceArea(A):
# Write your code here
# print(A)
# top and buttom
area = 2 * H * W
# checks the limit
def check(i, j):
return Ax+iy+j if 0 <= x+i < H and 0 <= y+j < W else 0 # remaining surfaces
for x in range(H):
for y in range(W):
# (0, 1), (0, -1), (1, 0), (1, 1)
for i, j in zip(xi, yi):
# gives area from adjacent cells
area += max(0, Axy - check(i, j)) return area
if __name__ == '__main__':
first_multiple_input = input().rstrip().split()
H = int(first_multiple_input0) W = int(first_multiple_input1) A = []
for _ in range(H):
A.append(list(map(int, input().rstrip().split())))
result = surfaceArea(A)
fptr.write(str(result) + '\n')
fptr.close()
メモ
https://www.youtube.com/watch?v=GYaLhyfGRhM
提出
code: python
import math
import os
import random
import re
import sys
#
# Complete the 'surfaceArea' function below.
#
# The function is expected to return an INTEGER.
# The function accepts 2D_INTEGER_ARRAY A as parameter.
#
def surfaceArea(A, H, W):
# Write your code here
# 9 + 9 + 11 + 10 + 10 + 11
topNum = H * W
downNum = H * W
front = []
frontAns = []
for a in A:
for i in range(len(A)):
for j, a in enumerate(Ai): if j == 0:
continue
else:
frontAns.append(a - fronti-1) frontNum = sum(frontAns)
B = []
back = []
backAns = []
for a in A:
B.append(list(reversed(a)))
for b in B:
for i in range(len(A)):
for j, b in enumerate(Bi): if j == 0:
continue
else:
backAns.append(b - backi-1) backNum = sum(backAns)
L = [[] for _ in range(len(A))]
left = []
leftAns = []
for i in range(len(A)):
for a in A:
for l in L:
for i in range(len(A)):
for j, l in enumerate(Li): if j == 0:
continue
else:
leftAns.append(l - lefti-1) leftNum = sum(leftAns)
R = []
right = []
rightAns = []
for l in L:
R.append(list(reversed(l)))
for r in R:
for i in range(len(A)):
for j, r in enumerate(Ri): if j == 0:
continue
else:
rightAns.append(r - righti-1) print(rightAns)
rightNum = sum(rightAns)
return topNum + downNum + frontNum + backNum + leftNum + rightNum
if __name__ == '__main__':
first_multiple_input = input().rstrip().split()
H = int(first_multiple_input0) W = int(first_multiple_input1) A = []
for _ in range(H):
A.append(list(map(int, input().rstrip().split())))
result = surfaceArea(A, H, W)
fptr.write(str(result) + '\n')
fptr.close()